package com.widget;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.ddd.wandemo.R;
import com.util.DensityUtils;

public class MenuItemView extends FrameLayout {
    private Context mContext;
    private View mItemView;
    private int mParentWidth;
    private int mParentHeight;
    private int count = 0;
    private TextView mTvCount;
    private ImageView mIvDecrease;
    private int mContentWidth;
    private int mContentHeight;

    public MenuItemView(@NonNull Context context) {
        this(context, null);
    }

    public MenuItemView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, -1);
    }

    public MenuItemView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        initView();
    }


    private void initView() {
        mParentWidth = getResources().getDisplayMetrics().widthPixels;
        mItemView = LayoutInflater.from(mContext).inflate(R.layout.view_menu_item, null, false);
        mTvCount = mItemView.findViewById(R.id.count);
        mIvDecrease = mItemView.findViewById(R.id.iv_decrease);

        mContentWidth = mParentWidth - DensityUtils.dp2px(mContext, 20);
        mContentHeight = DensityUtils.dp2px(mContext, 70);

        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(mContentWidth, mContentHeight);
        params.gravity = Gravity.CENTER;
        addView(mItemView, params);
        setBackgroundColor(getResources().getColor(R.color.Orange));

//        mItemView.setOnClickListener(new OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                addCount();
//            }
//        });
//
//        mIvDecrease.setOnClickListener(new OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                decrease();
//            }
//        });
    }

    public void decrease() {
        count -= 1;
        if (count <= 0) {
            mTvCount.setVisibility(GONE);
            mIvDecrease.setVisibility(GONE);
        } else {
            mTvCount.setText(String.valueOf(count));
        }
        mItemView.setBackgroundColor(getResources().getColor(R.color.Red));

        mItemView.getLayoutParams().width = mParentWidth;
        mItemView.getLayoutParams().height = mParentHeight;
        requestLayout();

        postDelayed(new Runnable() {
            @Override
            public void run() {
                mItemView.getLayoutParams().width = mContentWidth;
                mItemView.getLayoutParams().height = mContentHeight;
                if (count <= 0) {
                    mItemView.setBackgroundColor(Color.TRANSPARENT);
//                    mItemView.setBackground(getResources().getDrawable(R.drawable.bg_item_menu_unselect));
                } else {
//                    mItemView.setBackgroundColor(getResources().getColor(R.color.colorAccent));
                    mItemView.setBackground(getResources().getDrawable(R.drawable.bg_item_menu_select));
                }
                requestLayout();
            }
        }, 200);
    }

    public void addCount() {
        count += 1;
        if (count > 0) {
            mTvCount.setVisibility(VISIBLE);
            mIvDecrease.setVisibility(VISIBLE);
            mTvCount.setText(String.valueOf(count));
            mItemView.setBackgroundColor(getResources().getColor(R.color.colorAccent));
        }

        mItemView.getLayoutParams().width = mParentWidth;
        mItemView.getLayoutParams().height = mParentHeight;
        requestLayout();

        postDelayed(new Runnable() {
            @Override
            public void run() {
                mItemView.getLayoutParams().width = mContentWidth;
                mItemView.getLayoutParams().height = mContentHeight;
                mItemView.setBackground(getResources().getDrawable(R.drawable.bg_item_menu_select));
                requestLayout();
            }
        }, 200);
    }

    public void setItemView(int tempCount) {
        count = tempCount;
        if (count <= 0) {
            mTvCount.setVisibility(GONE);
            mIvDecrease.setVisibility(GONE);
            mTvCount.setText(String.valueOf(count));
            mItemView.setBackgroundColor(Color.TRANSPARENT);
//            mItemView.setBackground(getResources().getDrawable(R.drawable.bg_item_menu_unselect));
        }else {
            mTvCount.setVisibility(VISIBLE);
            mIvDecrease.setVisibility(VISIBLE);
            mTvCount.setText(String.valueOf(count));
//            mItemView.setBackgroundColor(getResources().getColor(R.color.colorAccent));
            mItemView.setBackground(getResources().getDrawable(R.drawable.bg_item_menu_select));
        }
    }


    public int getCount(){
        return this.count;
    }

    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mParentHeight = getHeight();
    }

    //    @Override
    //    public boolean onInterceptTouchEvent(MotionEvent event) {
    //        switch (event.getAction()){
    //            case MotionEvent.ACTION_DOWN:
    //                mStartY = event.getRawY();
    //                LogUtil.d("intercept_down");
    //                break;
    //            case MotionEvent.ACTION_MOVE:
    //                mXMove = event.getRawX();
    //                float diff = Math.abs(mXMove - mStartY);
    //                mStartY = mXMove;
    //                LogUtil.d("slop--"+Math.abs(mXMove - mStartY));
    //                if (diff > 60) {
    //                    setPadding(0, 0, 0, 0);
    //                    requestLayout();
    //                    return true;
    //                }
    //        }
    //        return super.onInterceptTouchEvent(event);
    //    }

    //    @Override
    //    public boolean onTouchEvent(MotionEvent event) {
    //        switch (event.getAction()) {
    //            case MotionEvent.ACTION_DOWN:
    //                mItemView.getLayoutParams().width = mParentWidth;
    //                mItemView.getLayoutParams().height = mParentHeight;
    //                requestLayout();
    //                break;
    //            case MotionEvent.ACTION_UP:
    //                mItemView.getLayoutParams().width = mParentWidth - DensityUtils.dp2px(mContext, 20);
    //                mItemView.getLayoutParams().height = DensityUtils.dp2px(mContext, 60);
    //                requestLayout();
    //                break;
    //        }
    //        return true;
    //    }
}
